from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from plone.memoize.instance import memoize
from BeautifulSoup import BeautifulSoup as bt

from my315ok.products import productsMessageFactory as _


class ImultiproductsView(Interface):
    """
    multiproducts view interface
    """

    def mainimage(self):
        """fetch the product main photo"""
    def auximage(self):
        """fetch attached photo for the product"""
    def parameters(self):
        """fetch product details parameters table"""    
           



class multiproductsView(BrowserView):
    """
    multiproducts browser view
    """
    implements(ImultiproductsView)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog')

    @property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()

    @memoize
    def prdt_images(self):
        catalog = getToolByName(self.context, 'portal_catalog')
        sepath= '/'.join(self.context.getPhysicalPath()) 
        query = {'meta_type':('product'),
                 'sort_on':'getObjPositionInParent',
                 'sort_order':'forward',
                 'path':sepath,
                 }        
        sd = catalog(query)
        return sd
    
    @memoize
    def img_tag(self,fieldname="image",small="tile",preview="mini",large="large"):
        imglists = {}
        csmall = []
        cpreview = []
        clargelink = []
        imgviewurl = []
        imgobjs = self.prdt_images()
        num = len(imgobjs)
        if num > 0:            
            for i in imgobjs:
                jt = i.getObject()
                try:
                    fd = jt.getField(fieldname)
                    csmall.append(fd.tag(jt,scale=small))
                    cpreview.append(fd.tag(jt,scale=preview))
                    clargelink.append(jt.absolute_url() + "/" + fieldname + "_" + large)
                    imgviewurl.append(jt.absolute_url() + "/base_view")
                except:
                    continue                
            
        imglists["small"] = csmall
        imglists["preview"] = cpreview
        imglists["large"] = clargelink
        imglists["imgurl"] = imgviewurl
        return imglists
    def mainimage(self,fieldname="image"):
        main = self.img_tag("image")
        return main
        
    def auximage(self,j):
        aux = self.details(fieldname="text")
        sp = bt(aux["comments"][j])
        try:
            auim = sp("img")[0].__str__()
        except:
            auim = ""
        return auim
        
    def parameters(self,j):
        aux = self.details(fieldname="text")
        sp = bt(aux["comments"][j])
       
        try:
            par = sp("table")[0].__str__()
        except:
            par = ""
        return par
        
        
    @memoize
    def details(self,fieldname="text"):
#        import pdb
#        pdb.set_trace()
        imglists = {}
        cpara = []       
        imgobjs = self.prdt_images()
        num = len(imgobjs)
        if num > 0:            
            for i in imgobjs:
                jt = i.getObject()
                try:
                    fd = jt.getText()
                    cpara.append(fd)                            
                except:
                    continue                
            
        imglists["comments"] = cpara        
        return imglists  
        
    
    
